package com.mc.lp.view.controllers;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mc.lp.database.cache.CacheServiceType;
import com.mc.lp.database.cache.DBCacheService;
import com.mc.lp.database.cache.ICacheDataProvider;
import com.mc.lp.service.impl.UserServiceImpl;
import com.mc.lp.shared.vo.IValueObject;
import com.mc.lp.view.constants.IConstants;

@Controller
public class StaticPageController implements IConstants{
	
	
	@Resource
	public UserServiceImpl userService;
	/*
	 * Following method will serve all direct page requests like home, login, contact us, email us, about us etc.
	 */
	@RequestMapping(value = "/view/{pageName}", method = RequestMethod.GET)
	public String getStaticPage(@PathVariable("pageName") String pageName) {
		return pageName;
	}
	
	@RequestMapping(value = "/", method = {RequestMethod.GET,RequestMethod.POST})
	public String getHomePage() {
		return "home";
	}
	
	@RequestMapping(value = "/view/shared/{pageName}", method = RequestMethod.GET)
	public String getSharedPage(@PathVariable("pageName") String pageName) {
		return "/shared/"+pageName;
	}
	
	@RequestMapping(value = "/view/admin/{pageName}", method = RequestMethod.GET)
	public String getAdminPage(@PathVariable("pageName") String pageName) {
		return "/admin/"+pageName;
	}
	@RequestMapping(value = "/view/user/{pageName}", method = RequestMethod.GET)
	public String getUserPage(@PathVariable("pageName") String pageName) {
		return "/user/"+pageName;
	}
	
	@RequestMapping(value = "/view/user/templates/{pageName}", method = RequestMethod.GET)
	public String getUserTemplatePage(@PathVariable("pageName") String pageName) {
		return "/user/templates/"+pageName;
	}
	
	
	/**
	 * currently we are only providing cache service with getAll and q
	 * @param entityName
	 * @param query
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/json/cache/{entity}/{q}", method = RequestMethod.GET, headers="Accept=application/json")
	public List<IValueObject> getCacheItem(@PathVariable("entity") String entityName, @PathVariable("q") String query, @RequestParam(value="qs",required=false) String queryString){
		CacheServiceType serviceRequested=CacheServiceType.valueOf(entityName.toUpperCase());
		//if given entity name not provided by cache then throw exception do not process the request.
		if(serviceRequested==null)
			throw new RuntimeException("Cache Service Not Found");
		DBCacheService service= getCacheService();
		ICacheDataProvider cache= service.getService(serviceRequested);
		//0 means user required all of the items,
		if(!StringUtils.isEmpty(queryString)){
			query=queryString; //assign new value to query from query string
		}
		if("0".equals(query)){
			return cache.getAll();
		}else	{
			return cache.search(query);
		}
	}
	
	/**
	 * currently we are only providing cache service with getAll and q
	 * @param entityName
	 * @param query
	 * @return
	 */

	public DBCacheService getCacheService() {
		return userService.getCacheService();
	}
}